/**
 * @class util
 * @this util
 * @type {*|exports|module.exports}
 */
var   log = require('./log'),
      config = require('./configs'),
      crypto = require('crypto'),
      validation = require('./common-validation'),
      vpnserver = require('./vpnserver'),
      weixin = require('./weixin'),
      sandbox = require('./sandbox'),
      util = {};

/**
 * 截取Openid的MD5散列值前8位，并转换为十进制形式(有可能是大于8为的10进制数, 用于生成推广二维码)
 * @param openid
 * @returns {Number}
 */
util.generatePromotionCode = function (openid) {
  var md5 = crypto.createHash('md5');
  md5.update(openid);
  return parseInt(md5.digest('hex').substr(0, 8), 16);
};

util.isIp = function (strIp) {
  return /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/.test(strIp);
};

/**
 * 取得一个数字的小数部分, 没有返回0
 * @param n
 * @returns {Number}
 */
util.getDecimal = function (n) {
  return parseFloat("0."+(String(n)).split(".")[1]);
};

/**
 * 取得一个数字的整数部分, 没有返回0
 * @param n
 * @returns {number}
 */
util.getInteger = function (n) {
  var ans = 0;
  if (n > 0) {
    ans = Math.floor(n);
  }  else {
    ans = Math.ceil(n);
  }
  return ans;
};

module.exports = util;
module.exports.config = config;
module.exports.validation = validation;
module.exports.weixin = weixin;
module.exports.vpnserver = vpnserver;
module.exports.sandbox = sandbox;
module.exports.log = log;
module.exports.encryptor = require('./encryptor');
